﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using Ylh.BLL;
using System.IO;

public partial class Module_NoteManger_NoteDetail : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BtnClose.OnClientClick = ActiveWindow.GetHidePostBackReference();
        if (!IsPostBack)
        {
            if (Request.QueryString["NoteID"]!=null)
            {
                 LoadData(Request.QueryString["NoteID"].ToString());
            }
           
        }

    }

    private void LoadData(string NoteID)
    {
        Ylh.BLL.NoteBLL bll = new NoteBLL(DbConnectionStr);
        Ylh.Model.TbNote note = bll.GetModel(Convert.ToInt32(NoteID));
        Summary.Text = note.Summary;
        Content.Text = note.Content;
        this.NoteID.Text = Request.QueryString["NoteID"].ToString();
    }



    private Ylh.Model.TbNote GetNote()
    {
        Ylh.Model.TbNote model = new Ylh.Model.TbNote();
        model.Summary = Summary.Text;
        model.CreateTime = DateTime.Now;
        model.CreateName = StaffName;
        model.FilePath = FilePath.Text;
        model.state = true;
        model.Content = Content.Text;
        if (!string.IsNullOrEmpty(NoteID.Text))
        {
            model.NoteID =Convert.ToInt32( NoteID.Text);
        }

        return model;
    }

    /// <summary>
    /// 上传附件
    /// </summary>
    /// <returns></returns>
    public string UploadAccessory(ref string FileExtension, ref string strdtime)
    {
        System.Web.HttpPostedFile postedFile = FilePath.PostedFile;
        //检查存放路径是否存在，如果不存在创建一个目录

        string strSysMap = "../../Files/Note/";
        DirectoryInfo dirInfo = new DirectoryInfo(MapPath(strSysMap));
        if (!dirInfo.Exists)
        {
            dirInfo.Create();
        }
        //上传数据
        strdtime = DateTime.Now.ToString("yyyyMMddHHmmss");
        //获取上传文件的扩展名
        FileExtension = System.IO.Path.GetExtension(postedFile.FileName).ToLower();
        string strpath = Server.MapPath("~/Files/Note/");
      string  Strfile = strdtime + FileExtension;
        postedFile.SaveAs(strpath + Strfile);
        return "~/Files/Note/" + Strfile;
    }




    protected void BtnSave_Click(object sender, EventArgs e)
    {
        //if (!FilePath.HasFile)
        //{
        //    Alert.Show("请先选择需要上传的文件");
        //    return;
        //}

        if (string.IsNullOrEmpty(Summary.Text))
        {
            Alert.Show("公告概要不能为空");
            return;
        }

        Ylh.Model.TbNote model = GetNote();

        //string FileExtension = string.Empty;

        //string Strfile = string.Empty;

        //string path = UploadAccessory(ref FileExtension, ref Strfile);

        //if (FileExtension.ToLower() != ".doc")
        //{
        //    Alert.Show("上传文件格式不对");
        //    return;
        //}

        //string locopath = Server.MapPath(path);

        //string pathHTML = locopath.Remove(locopath.IndexOf(FileExtension)) + ".html";
        //model.FilePath = "../../Files/Note/" + Strfile+".html";
        Ylh.BLL.NoteBLL note = new NoteBLL(DbConnectionStr);

        if (string.IsNullOrEmpty(this.NoteID.Text))
        {
            NoteID.Text = note.InsertNote(model).ToString();
        }
        else
        {
            note.UpdateNote(model);
        }
        //Ylh.Office.WordXLS doc = new Ylh.Office.WordXLS();

        //doc.WordToHTML(Server.MapPath(path), pathHTML);

        PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
    }
}